<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class post_model extends CI_Model {
	var $table = "jonu_post";
	var $setting;

	public function __construct(){
		parent::__construct();
		$this->setting = $this->settings();
	}

	public function count_all(){
		$query = $this->db->count_all_results($this->table);
		return $query;
	}
	public function count_published(){
		$this->db->where('jonu_post_status', "Published");
		$query = $this->db->count_all_results($this->table);
		return $query;	
	}
	public function count_pending(){
		$this->db->where('jonu_post_status', "Pending");
		$query = $this->db->count_all_results($this->table);
		return $query;	
	}
	public function count_blocked(){
		$this->db->where('jonu_post_status', "Blocked");
		$query = $this->db->count_all_results($this->table);
		return $query;	
	}

	public function home_post(){
		$this->db->join('jonu_user', 'jonu_post.jonu_user_id = jonu_user.jonu_user_id');		
		$this->db->where('jonu_post_status', 'Published');
		$this->db->order_by("jonu_post_id", "desc"); 
		$query = $this->db->get($this->table);
		$result = $query->result_array();

		return $result;
	}

	public function single($id, $permalink){
		$this->db->join('jonu_user', 'jonu_post.jonu_user_id = jonu_user.jonu_user_id');
		$this->db->where('jonu_post_id', $id);
		$this->db->where('jonu_post_permalink', $permalink);
		$query = $this->db->get($this->table);
		$result = $query->result_array();

		return $result;
	}

	public function home_post_paged($num, $offset){		
		$this->db->join('jonu_user', 'jonu_post.jonu_user_id = jonu_user.jonu_user_id');		
		$this->db->where('jonu_post_status', 'Published');
		$this->db->order_by("jonu_post.jonu_post_id", "desc"); 		
		$query = $this->db->get($this->table, $num, $offset);
		$result = $query->result_array();
		return $result;
	}

	public function get_all_rand(){
		$this->db->where('jonu_post_status', 'Published');
		$this->db->order_by('jonu_post_view', 'desc');
		$query = $this->db->get($this->table, $this->setting['recommend_post']);
		$result = $query->result_array();

		return $result;
	}

	public function get_by_cat($cat_slug){
		$this->db->join('jonu_category', 'jonu_pc.jonu_category_id = jonu_category.jonu_category_id');
		$this->db->join('jonu_post', 'jonu_pc.jonu_post_id = jonu_post.jonu_post_id');
		$this->db->join('jonu_user', 'jonu_post.jonu_user_id = jonu_user.jonu_user_id');
		$this->db->where('jonu_category_slug', $cat_slug);
		$query = $this->db->get('jonu_pc');
		$result = $query->result_array();

		return $result;
	}

	public function get_pow(){
		$this->db->join('jonu_pc', 'jonu_pc.jonu_post_id = jonu_post.jonu_post_id');
		$this->db->where('jonu_post_status', 'Published');
		$this->db->where('jonu_category_id', 9);
		$this->db->order_by("jonu_post.jonu_post_id", "desc"); 
		$query = $this->db->get($this->table);
		$result = $query->result_array();

		return $result;
	}

	public function pow_paged($num, $offset){
		$this->db->join('jonu_user', 'jonu_post.jonu_user_id = jonu_user.jonu_user_id');		
		$this->db->join('jonu_pc', 'jonu_post.jonu_post_id = jonu_pc.jonu_post_id');
		$this->db->where('jonu_post_status', 'Published');
		$this->db->where('jonu_category_id', 9);
		$this->db->order_by("jonu_post.jonu_post_id", "desc"); 		
		$query = $this->db->get($this->table, $num, $offset);
		$result = $query->result_array();
		return $result;
	}

	public function get_by_user($user_id){
		$this->db->where('jonu_user_id', $user_id);
		$this->db->order_by("jonu_post.jonu_post_id", "desc"); 
		$query = $this->db->get($this->table);

		$result = $query->result_array();

		return $result;
	}

	public function post_by_user_paged($num, $offset){
		$this->db->where('jonu_user_id', get_cookie('c_usr_id'));
		$this->db->order_by("jonu_post.jonu_post_id", "desc"); 		
		$query = $this->db->get($this->table, $num, $offset);
		$result = $query->result_array();
		return $result;
	}

	public function get_by_id($post_id){
		$this->db->join('jonu_user', 'jonu_user.jonu_user_id = jonu_post.jonu_user_id');
		$this->db->where('jonu_post_id', $post_id);		
		$query = $this->db->get($this->table);
		$result = $query->result_array();

		return $result;
	}

	public function store($param){
		if($param['foto'] != ""){
			$setting = $this->post_settings();
			$data = array(
				'jonu_user_id'		=> get_cookie('c_usr_id'),
				'jonu_post_title'	=> $param['post_title'],
				'jonu_post_content'	=> $param['post_content'],
				'jonu_post_date'	=> date('Y-m-d m:s'),
				'jonu_post_status'	=> $setting['post_status'],
				'jonu_post_image'	=> $param['foto'],
				'jonu_post_permalink'	=> $param['permalink'],
				'jonu_post_location'	=> $param['post_location'],
				'jonu_post_latitude'	=> $param['post_latitude'],
				'jonu_post_longitude'	=> $param['post_longitude'],
				'jonu_post_view'	=> 0
				);
			$query = $this->db->insert($this->table, $data);
			
			if($query){
				$this->db->select_max('jonu_post_id', 'topid');
				$query2 = $this->db->get('jonu_post');
				$result2 = $query2->result_array();
				$max_id = $result2[0]['topid'];

				$data2 = array();
				for($j=0; $j<count($param['category']); $j++){
					$data2[$j]['jonu_post_id'] =  $max_id;
					$data2[$j]['jonu_category_id'] =  $param['category'][$j];
				}

				for($k=0; $k<count($data2); $k++){
					$query2 = $this->db->insert('jonu_pc', $data2[$k]);
				}

				return $query2;
			}else{
				return false;
			}
		}else{
			return false;
		}	
	}

	public function update_by_adm($param){
		$this->db->where('jonu_post_id', $param['post_id']);
		$q_del = $this->db->delete('jonu_pc');
		if($q_del){
			$data = array();
			for($j=0; $j<count($param['category']); $j++){
				$data[$j]['jonu_post_id'] =  $param['post_id'];
				$data[$j]['jonu_category_id'] =  $param['category'][$j];
			}

			for($k=0; $k<count($data); $k++){
				$q_ins = $this->db->insert('jonu_pc', $data[$k]);
			}

			
			if(isset($param['foto'])){
				$data2 = array(
				'jonu_post_title'	=> $param['post_title'],
				'jonu_post_content'	=> $param['post_content'],
				'jonu_post_location'	=> $param['post_location'],
				'jonu_post_latitude'	=> $param['post_latitude'],
				'jonu_post_longitude'	=> $param['post_longitude'],
				'jonu_post_image'	=> $param['foto']
				);
			}else{
				$data2 = array(
				'jonu_post_title'	=> $param['post_title'],
				'jonu_post_content'	=> $param['post_content'],
				'jonu_post_location'	=> $param['post_location'],
				'jonu_post_latitude'	=> $param['post_latitude'],
				'jonu_post_longitude'	=> $param['post_longitude']
				);
			}

			$this->db->where('jonu_post_id', $param['post_id']);
			if ($this->db->update($this->table, $data2)) {
				return true;
			}else{
				return false;
			}
		}else{
			echo "gagal";
		}
	}

	public function get_blocked(){		
		$this->db->join('jonu_user', 'jonu_post.jonu_user_id = jonu_user.jonu_user_id');
		$this->db->where('jonu_post_status', 'Blocked');
		$this->db->order_by("jonu_post_id", "desc"); 
		$query = $this->db->get($this->table);
		$result = $query->result_array();

		return $result;
	}
	public function block($id){
		$data = array(
			'jonu_post_status' => 'Blocked'
			);
		$this->db->where('jonu_post_id', $id);
		$query = $this->db->update($this->table, $data);

		return $query;
	}

	public function approve($id){
		$data = array(
			'jonu_post_status' => 'Published'
			);
		$this->db->where('jonu_post_id', $id);
		$query = $this->db->update($this->table, $data);

		return $query;
	}

	public function get_pending(){		
		$this->db->join('jonu_user', 'jonu_post.jonu_user_id = jonu_user.jonu_user_id');
		$this->db->where('jonu_post_status', 'Pending');
		$this->db->order_by("jonu_post_id", "desc"); 
		$query = $this->db->get($this->table);
		$result = $query->result_array();

		return $result;
	}
	public function pending($id){
		$data = array(
			'jonu_post_status' => 'Pending'
			);
		$this->db->where('jonu_post_id', $id);
		$query = $this->db->update($this->table, $data);

		return $query;
	}
	public function destroy($id){
		$this->db->where('jonu_post_id', $id);
		$q1 = $this->db->get($this->table);
		$res1 = $q1->result_array();		
		
		$this->db->where('jonu_post_id', $id);
		$q1 = $this->db->delete('jonu_pc');
		if($q1){
			$this->db->where('jonu_post_id', $id);
			$q2 = $this->db->delete('jonu_post');
			if($q2){
				if($res1[0]['jonu_post_image'] != ""){
					$img_path = $res1[0]['jonu_post_image'];
					$del_post_img = unlink("./asset/img/posts-image/$img_path");
					if($del_post_img){
						return true;
					}else{
						return false;
					}
				}
			}else{
				return false;
			}
		}else{
			return false;
		}
	}

	public function like($id, $user_id){
		$this->db->where('jonu_post_id', $id);
		$this->db->where('jonu_user_id', $user_id);
		$this->db->delete('jonu_dislike');

		$data = array(
			'jonu_user_id'	=> $user_id,
			'jonu_post_id'	=> $id
			);
		$query = $this->db->insert('jonu_like', $data);
	}

	public function dislike($id, $user_id){
		$this->db->where('jonu_post_id', $id);
		$this->db->where('jonu_user_id', $user_id);
		$this->db->delete('jonu_like');

		$data = array(
			'jonu_user_id'	=> $user_id,
			'jonu_post_id'	=> $id
			);
		$query = $this->db->insert('jonu_dislike', $data);		
	}

	private function post_settings(){
		$this->load->model('settings_model');

		$q_per_page_setting = $this->settings_model->get_all();
		foreach($q_per_page_setting as $q_per_page_setting_rec){
			if($q_per_page_setting_rec['jonu_setting_id'] == "adm_pagination"){
				$setting['home_pagination'] = $q_per_page_setting_rec['jonu_setting_value'];
			}else if($q_per_page_setting_rec['jonu_setting_id'] == "post_status"){
				$setting['post_status'] = $q_per_page_setting_rec['jonu_setting_value'];
			}
		}

		return $setting;
	}

	private function settings(){
		$this->load->model('settings_model');

		$q_per_page_setting = $this->settings_model->get_all();
		foreach($q_per_page_setting as $q_per_page_setting_rec){
			if($q_per_page_setting_rec['jonu_setting_id'] == "home_pagination"){
				$setting['home_pagination'] = $q_per_page_setting_rec['jonu_setting_value'];
			}
			if($q_per_page_setting_rec['jonu_setting_id'] == "recom_post"){
				$setting['recommend_post'] = $q_per_page_setting_rec['jonu_setting_value'];
			}
		}

		return $setting;
	}
}

/* End of file post_model.php */
/* Location: ./application/models/post_model.php */